﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TravianHelper.hero;
using System.Collections;

namespace TravianHelper.app
{
    /**
     * Store common data of the application such as Hero.
     * Most of the data are singleton
     **/ 
    public class TravianApplicationContext
    {
        public static bool loggedIn = false;
        public static HeroInfo heroInfo;
        public static HeroConfig heroConfig;
        public static HeroManager heroManager;
        public static Travian adapter;
        public static HeroPopup heroPopup;
        public static AttackForm attackForm;
        public static Form2 mainForm;

        public static ArrayList villages = new ArrayList();
        public static ArrayList allOasis = new ArrayList();
        public static ArrayList oasis = new ArrayList();
        public static ArrayList farmlists = new ArrayList();

        public static Hashtable errors = new Hashtable();

        public TravianApplicationContext()
        {
            // 1. Contruct object
            adapter = new Travian();
            heroPopup = new HeroPopup();
            heroManager = new HeroManager();
            attackForm = new AttackForm();
        }

        public static void loadData()
        {
            heroConfig = heroManager.loadHeroConfigFromForm();
            if (heroConfig == null)
                heroConfig = new HeroConfig();
            try
            {
                TravianApplicationContext.oasis = Util.DeSerializeArrayList("data.xml");
            }
            catch (Exception ex)
            {
                TravianApplicationContext.oasis = new ArrayList();
            }
            try
            {
                TravianApplicationContext.farmlists = Util.DeSerializeArrayList("farmlists.xml");
            }
            catch (Exception ex)
            {
                TravianApplicationContext.farmlists = new ArrayList();
            }
            try
            {
                TravianApplicationContext.allOasis = Util.DeSerializeArrayList("oasis.xml");
            }
            catch (Exception ex)
            {
                TravianApplicationContext.allOasis = new ArrayList();
            }
            try
            {
                TravianApplicationContext.villages = Util.DeSerializeArrayList("villages.xml");
            }
            catch (Exception ex)
            {
                TravianApplicationContext.villages = new ArrayList();
            }
            // 2. load Hero info
            heroInfo = heroManager.loadHeroInfo();
        }
    }
}
